﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DFYE.Web.Areas.manage.Controllers
{
    using DFYE.Web.Models;
    using JULONG.WEB.Library;
    public class studentDailyController : Controller
    {
        //
        // GET: /manage/studentDaily/
        DataSession<entityDBContainer, studentDaily> db = new DataSession<entityDBContainer, studentDaily>();
        public ActionResult Index(int id=0)
        {
            if (id == 0) {
                return RedirectToAction("students");
            }
            ViewBag.student = db.newSession<student>().get(d => d.ID == id);
            return View(db.query(d=>d.studentID==id));
        }

        public ActionResult students()
        {
            return View(db.newSession<student>().query(d=>d.able==true));
        }

        //
        // GET: /manage/studentDaily/Create

        public ActionResult Create(int id)
        {
            ViewBag.student = db.newSession<student>().get(d => d.ID == id);
            ViewBag.studentDailyType = db.newSession<studentDailyType>().query().ToList();
            return View();
        } 

        //
        // POST: /manage/studentDaily/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            studentDaily st = new studentDaily();
            TryUpdateModel(st);
            if (st.title == null) {
                st.title = "-";
            }
            try
            {
                db.insert(st);
                st.student.lastDate = DateTime.Now;
                db.save();

                return RedirectToAction("Index/"+st.studentID);
            }
            catch
            {
                ViewBag.student = db.newSession<student>().get(d => d.ID == st.studentID);
                ViewBag.studentDailyType = db.newSession<studentDailyType>().query().ToList();
                return View(st);
            }
        }
        
        //
        // GET: /manage/studentDaily/Edit/5
 
        public ActionResult Edit(int id)
        {
            studentDaily st = db.get(d => d.ID == id);
            ViewBag.studentDailyType = db.newSession<studentDailyType>().query().ToList();
            ViewData.Model = st;
            return View();
        }

        //
        // POST: /manage/studentDaily/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            studentDaily st = db.get(d => d.ID == id);
            TryUpdateModel(st);
            if (st.title == null)
            {
                st.title = "-";
            }
            try
            {
                db.save();

                return RedirectToAction("Index", new { id = st.student.ID });

            }
            catch
            {
                return View(st);
            }
        }


        //
        // POST: /manage/studentDaily/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            studentDaily st = db.get(d => d.ID == id);
            int studentID =st.studentID;
            try
            {
                

                db.delete(d => d.ID == id);

                // TODO: Add delete logic here

                return RedirectToAction("Index", new { id = studentID });
            }
            catch
            {
                return RedirectToAction("Index", new { id = studentID });
            }
        }
    }
}
